Dynomotion

Group: DynoMotion Message: 518 From: ekonradsson Date: 7/8/2010
Subject: How to use the PWM outputs on KFLOP?
I'm using KFLOP to control a stepper controller with a built in charge pump I need to enable. I've been using the built-in PWM puts but I have not found any info on how to use the clock dividers to calculate the proper frequency I need, 25Khz. I'm using a demo pwm output program that comes with the setup that makes a 30Khz signal but there is very little info on how to use this.

Any help greatly appreciated.

EK
Group: DynoMotion Message: 519 From: Tom Kerekes Date: 7/9/2010
Subject: Re: How to use the PWM outputs on KFLOP?
Hi EK,
 
Yes sorry there isn't much documentation on them.  They are defined in the KMotionDef.h file as:
 
// Digital I/O bit PWM control (8 I/O bits on KFlop JP8 may be pulsed)
#define N_IO_PWMS 8             // Number of pwms that may be assigned to GPIO bits  
#define IO_PWMS 0xD0            // FPGA offset to IO PWM registers (2 bytes each - value, enable(bit0))
#define IO_PWMS_PRESCALE 0x2f   // FPGA offset to IO PWM Pre-Scale clock divider 0-255, 0 = 16.6MHz, 1=8.33MHz, ...
#define IO_PWM_MAX_VALUE 255    // 0 = 0%, 255 = 100 % duty cycle

They will not work at 25 KHz.  They are 8-bit pwms that are driven by a 16.66MHz clock.  So the base frequency is 16.66MHz/256 = 65.08KHz.  However this can be divided down by an integer value of 1-256 using the Global Pre-scaler set to values of 0-255. There is only one pre-scaler that sets the PWM rates for all 8 PWMs.  So if you set the pre-scaler byte to a value of 2 it will divide the clock by 3 and all the PWM frequencys will be 65.08KHz/3 = 21.7KHz.  I'd be surprised if your charge pump requires exactly 25KHz.
 
Currently the only way to use the PWMs is from a KFlop C Program.  There are 8 PWMS.  Each PWM has 2 byte addresses associated with it.  So all 8 PWMs occupy a total of 16 consecutive byte addresses starting at IO_PWMS.  The first byte sets the duty cycle.  A value of 0 will solid low 0%, a value of 128 will be 50% duty cycle, and a value of 255 will be solid high 100%.  The second byte has only one significant bit used to enable the PWM and change the corresponding I/O bit from a General Purpose I/O bit to a PWM bit.
 
There is also the one byte address to set the Pre-Scaler (IO_PWM_PRESCALE)
 
The associated I/O bit must be configured a an output.
 
So as an example to setup PWM0 as a 25% duty cycle at 21.7KHz do the following:
 
 SetBitDirection(26,1);    // Set bit 26 (PWM 0 as an output)
 FPGA(IO_PWMS_PRESCALE) = 2;   // set pwm frequency 21.7KHz KHz
 FPGA(IO_PWMS+1) = 1;   // enable the PWM0
 FPGA(IO_PWMS+0) = 64;  // Set duty cycle to 25%
 
Another option would be to use a Step/Dir Generator.  In that case you could generate a 4us pulses at exactly 25KHz.
 
I hope this helps.
TK
 
 

Group: DynoMotion Message: 520 From: ekonradsson Date: 7/9/2010
Subject: Re: How to use the PWM outputs on KFLOP?
Thanks Tom, that explains it well. I actually did manage to use the Step / Dir generator to create the signal yesterday, but mostly by trial end error.

EK

--- In DynoMotion@yahoogroups.com, Tom Kerekes <tk@...> wrote:
>
> Hi EK,
>
> Yes sorry there isn't much documentation on them.  They are defined in the
> KMotionDef.h file as:
>
> // Digital I/O bit PWM control (8 I/O bits on KFlop JP8 may be pulsed)
> #define N_IO_PWMS 8             // Number of pwms that may be assigned to GPIO
> bits  
> #define IO_PWMS 0xD0            // FPGA offset to IO PWM registers (2 bytes each
> - value, enable(bit0))
> #define IO_PWMS_PRESCALE 0x2f   // FPGA offset to IO PWM Pre-Scale clock divider
> 0-255, 0 = 16.6MHz, 1=8.33MHz, ...
> #define IO_PWM_MAX_VALUE 255    // 0 = 0%, 255 = 100 % duty cycle
>
> They will not work at 25 KHz.  They are 8-bit pwms that are driven by a 16.66MHz
> clock.  So the base frequency is 16.66MHz/256 = 65.08KHz.  However this can be
> divided down by an integer value of 1-256 using the Global Pre-scaler set to
> values of 0-255. There is only one pre-scaler that sets the PWM rates for all 8
> PWMs.  So if you set the pre-scaler byte to a value of 2 it will divide the
> clock by 3 and all the PWM frequencys will be 65.08KHz/3 = 21.7KHz.  I'd be
> surprised if your charge pump requires exactly 25KHz.
>
> Currently the only way to use the PWMs is from a KFlop C Program.  There are 8
> PWMS.  Each PWM has 2 byte addresses associated with it.  So all 8 PWMs occupy a
> total of 16 consecutive byte addresses starting at IO_PWMS.  The first byte sets
> the duty cycle.  A value of 0 will solid low 0%, a value of 128 will be 50% duty
> cycle, and a value of 255 will be solid high 100%.  The second byte has only one
> significant bit used to enable the PWM and change the corresponding I/O bit from
> a General Purpose I/O bit to a PWM bit.
>
> There is also the one byte address to set the Pre-Scaler (IO_PWM_PRESCALE)
>
> The associated I/O bit must be configured a an output.
>
> So as an example to setup PWM0 as a 25% duty cycle at 21.7KHz do the following:
>
>  SetBitDirection(26,1);    // Set bit 26 (PWM 0 as an output)
>  FPGA(IO_PWMS_PRESCALE) = 2;   // set pwm frequency 21.7KHz KHz
>  FPGA(IO_PWMS+1) = 1;   // enable the PWM0
>  FPGA(IO_PWMS+0) = 64;  // Set duty cycle to 25%
>
>
> Another option would be to use a Step/Dir Generator.  In that case you could
> generate a 4us pulses at exactly 25KHz.
>
> I hope this helps.
> TK
>
>
>
>
>
> ________________________________
> From: ekonradsson <erlendur.konradsson@...>
> To: DynoMotion@yahoogroups.com
> Sent: Thu, July 8, 2010 7:36:12 PM
> Subject: [DynoMotion] How to use the PWM outputs on KFLOP?
>
>  
> I'm using KFLOP to control a stepper controller with a built in charge pump I
> need to enable. I've been using the built-in PWM puts but I have not found any
> info on how to use the clock dividers to calculate the proper frequency I need,
> 25Khz. I'm using a demo pwm output program that comes with the setup that makes
> a 30Khz signal but there is very little info on how to use this.
>
> Any help greatly appreciated.
>
> EK
>